;
(function (window, angular) {
	'use strict';
	angular.module("Controller.Corruption.PriceSchemeWarn", [])
		.controller("PriceSchemeWarnCtrl", ["$scope", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService","$stateParams", function ($scope, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService,$stateParams) {
			$scope.handleMultiInput = function (e) {
				$scope.condition.materialsNos = UserContextService.MultiInput(e.target.value);
			};
			//
			$scope.handleMaterielMultiInput = function (e) {
				$scope.condition.materialsNos = UserContextService.MultiInput(e.target.value);
			};

			$scope.subTabFlag = [true, false];
			$scope.placeholder = '客户编码/客户名称';
			$scope.markInfo = {
				allChecked: false,
				curRow: -1
			};
			$scope.pageInfo = {
				totalPages: 1,
				totalRows: 0,
				pageSize: 10,
				curPage: 1
			};
			//
			$scope.pageInfoMateriel = {
				totalPages: 1,
				totalRows: 0,
				pageSize: 10,
				curPage: 1
			};
			$scope.toolbarConfig = {
				showColConfig: false
			};
			$scope.toolbarConfigMateriel = {
				showColConfig: false
			};
			$scope.condition = { status: 0 };
			//
			$scope.conditionMateriel = { status: 0 };
			//客户
			$scope.dataList = [];
			$scope.tableConfig = {
				fixedWidth: 100,
				scrollWidth: 1400,
				tableCol: [
					{
						name: "客户编码",
						dataField: "customerCode",
						width: 150,
						isShow: true,
						isFixed: false,
					},
					{
						name: "客户名称",
						dataField: "customerName",
						width: 200,
						isShow: true,
						isFixed: false,
					},
					{
						name: "价格方案名称",
						dataField: "priceSchemeName",
						width: 120,
						isShow: true,
						isFixed: false,
					},
					{
						name: "价格方案类型",
						dataField: "priceSchemeTypeShow",
						width: 150,
						isShow: true,
						isFixed: false,
					},
					{
						name: "状态",
						dataField: "priceSchemeStatus",
						width: 120,
						isShow: true,
						isFixed: false,
					},
					{
						name: "是否删除",
						dataField: "priceSchemeDelFlag",
						width: 140,
						isShow: true,
						isFixed: false,
					},
					{
						name: "创建时间",
						dataField: "createTime",
						width: 140,
						isShow: true,
						isFixed: false,
					},
					{
						name: "创建人",
						dataField: "creator",
						width: 140,
						isShow: true,
						isFixed: false,
					},
					{
						name: "更新时间",
						dataField: "updateTime",
						width: 140,
						isShow: true,
						isFixed: false,
					},
					{
						name: "更新人",
						dataField: "modifier",
						width: 120,
						isShow: true,
						isFixed: false,
					},
					{
						name: "单据状态",
						dataField: "stateShow",
						width: 120,
						isShow: true,
						isFixed: false,
					},
					{
						name: "价格方案说明",
						dataField: "priceSchemeRemark",
						width: 180,
						isShow: true,
						isFixed: false,
					},
					{
						type: 'operate',
						name: "操作",
						width: 60,
						isShow: true,
						isFixed: true,
					},
				]
			};
			//零件
			$scope.dataListMateriel = [];
			$scope.tableConfigMateriel = {
				fixedWidth: 100,
				scrollWidth: 1400,
				tableCol: [
					{
						name: "零件编号",
						dataField: "materialsNo",
						width: 120,
						isShow: true,
						isFixed: false,
					},
					{
						name: "零件中文名",
						dataField: "materialsDes",
						width: 200,
						isShow: true,
						isFixed: false,
					},
					{
						name: "价格方案名称",
						dataField: "priceSchemeName",
						width: 120,
						isShow: true,
						isFixed: false,
					},
					{
						name: "价格方案类型",
						dataField: "priceSchemeTypeShow",
						width: 120,
						isShow: true,
						isFixed: false,
					},
					{
						name: "状态",
						dataField: "priceSchemeStatus",
						width: 120,
						isShow: true,
						isFixed: false,
					},
					{
						name: "是否删除",
						dataField: "priceSchemeDelFlag",
						width: 140,
						isShow: true,
						isFixed: false,
					},
					{
						name: "创建时间",
						dataField: "createTime",
						width: 140,
						isShow: true,
						isFixed: false,
					},
					{
						name: "创建人",
						dataField: "creator",
						width: 140,
						isShow: true,
						isFixed: false,
					},
					{
						name: "更新时间",
						dataField: "updateTime",
						width: 140,
						isShow: true,
						isFixed: false,
					},
					{
						name: "更新人",
						dataField: "modifier",
						width: 120,
						isShow: true,
						isFixed: false,
					},
					{
						name: "单据状态",
						dataField: "stateShow",
						width: 120,
						isShow: true,
						isFixed: false,
					},
					{
						name: "价格方案说明",
						dataField: "priceSchemeRemark",
						width: 180,
						isShow: true,
						isFixed: false,
					},
					{
						type: 'operate',
						name: "操作",
						width: 60,
						isShow: true,
						isFixed: true,
					},
				]
			};

			$scope.setFixedWidth = function () {
				$scope.tbStyle = {
					scrollLeft: { left: $scope.tableConfig.fixedWidth + "px" }
				};
			};

			$scope.exportParam = {
				sectionParam: { ids: [] },
				allUrl: ybHttp.RequestUrl("device", "/priceSchemeWarnCustomer/exportAll")
			};

			$scope.reset = function () {
				$scope.condition = {};
			};

			$scope.reset2 = function () {
				$scope.conditionMateriel = {};
			};
			//客户
			function getDataList(curPage) {
				$scope.markInfo.allChecked = false;
				$scope.markInfo.curRow = -1;
				$scope.exportParam.sectionParam.ids = [];

				var service = "device";
				var reqUrl = "/priceSchemeWarnCustomer/list";
				if ($scope.subTabFlag[1]) {
					reqUrl = "/priceSchemeWarnMateriel/list";
				}
				if (!!curPage) {
					$scope.pageInfo.curPage = curPage;
				}

				var param = {
					pageSize: $scope.pageInfo.pageSize,
					pageNum: $scope.pageInfo.curPage,
					filters: angular.copy($scope.condition)
				};
				//
				ybHttp.Post(service, reqUrl, param).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return;
					}
					angular.forEach(resp.value.list, function (item) {
						item.isChecked = false;
						item.createTime = $filter("YbDate")(item.createTime, 'yyyy-MM-dd HH:mm:ss'); // 单据创建时间
						item.updateTime = $filter("YbDate")(item.updateTime, 'yyyy-MM-dd HH:mm:ss'); // 单据修改时间
						item.stateShow = $filter("ProgressStatus")(item.status, 'priceChangeInstru');//单据类型
						item.priceSchemeDelFlag = $filter("ProgressStatus")(item.priceSchemeDelFlag, 'deleteState');//单据类型
						item.priceSchemeStatus = $filter("TrueOrFalse")(item.priceSchemeStatus, "enabled");//价格方案状态
						item.priceSchemeTypeShow = $filter("TrueOrFalse")(item.priceSchemeType, "schemeType");//价格方案类型
						// item.adjustDateShow = $filter("YbDate")(item.adjustDate, 'yyyy-MM-dd HH:mm:ss');
						// item.jackConShow = $filter("ProgressStatus")(item.jackCon, 'priceChangeState');
					});
					$scope.pageInfo.totalRows = resp.value.total;
					$scope.pageInfo.totalPages = resp.value.pages;
					$scope.dataList = resp.value.list;
				});
			}

			function getDataListMateriel(curPage) {
				$scope.markInfo.allChecked = false;
				$scope.markInfo.curRow = -1;
				$scope.exportParam.sectionParam.ids = [];

				var service = "device";
				var reqUrl = "/priceSchemeWarnMateriel/list";
				if (!!curPage) {
					$scope.pageInfoMateriel.curPage = curPage;
				}
				var param = {
					pageSize: $scope.pageInfoMateriel.pageSize,
					pageNum: $scope.pageInfoMateriel.curPage,
					filters: angular.copy($scope.conditionMateriel)
				};

				ybHttp.Post(service, reqUrl, param).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return;
					}
					angular.forEach(resp.value.list, function (item) {
						item.isChecked = false;
						item.createTime = $filter("YbDate")(item.createTime, 'yyyy-MM-dd HH:mm:ss'); // 单据创建时间
						item.updateTime = $filter("YbDate")(item.updateTime, 'yyyy-MM-dd HH:mm:ss'); // 单据修改时间
						item.stateShow = $filter("ProgressStatus")(item.status, 'priceChangeInstru');//单据类型
						item.priceSchemeDelFlag = $filter("ProgressStatus")(item.priceSchemeDelFlag, 'deleteState');//单据类型
						item.priceSchemeStatus = $filter("TrueOrFalse")(item.priceSchemeStatus, "enabled");//价格方案状态
						item.priceSchemeTypeShow = $filter("TrueOrFalse")(item.priceSchemeType, "schemeType");//价格方案类型
					});
					$scope.pageInfoMateriel.totalRows = resp.value.total;
					$scope.pageInfoMateriel.totalPages = resp.value.pages;
					$scope.dataListMateriel = resp.value.list;
				});
			}



			$scope.switchToolbar = function (toolbar) {
				if ($scope.subTabFlag[0]) {
					if (!!$scope.toolbarConfig["show" + toolbar]) {
						$scope.toolbarConfig["show" + toolbar] = false;
					} else {
						$timeout(function () {
							$scope.toolbarConfig["show" + toolbar] = true;
						});
					}
				} else {
					if (!!$scope.toolbarConfigMateriel["show" + toolbar]) {
						$scope.toolbarConfigMateriel["show" + toolbar] = false;
					} else {
						$timeout(function () {
							$scope.toolbarConfigMateriel["show" + toolbar] = true;
						});
					}
				}
			};

			$scope.selectRow = function (index) {
				$scope.markInfo.curRow = index
			};

			$scope.search = function (curpage) {
				if ($scope.subTabFlag[1]) {
					getDataListMateriel(curpage);
				} else {
					getDataList(curpage);
				}
			};

			function getBillIds() {
				$scope.exportParam.sectionParam.ids = [];
				angular.forEach($scope.dataList, function (item) {
					if (!!item.isChecked) {
						$scope.exportParam.sectionParam.ids.push(item.id);
					}
				});
			}

			$scope.selectAll = function (list, type) {
				for (var i = 0, len = list.length; i < len; i++) {
					list[i].isChecked = angular.isUndefined(type) ? $scope.markInfo.allChecked : $scope.markInfo["allChecked" + type];
				}
				getBillIds();
			};

			function checkAll(list) {
				var selectAll = false;
				for (var i = 0, len = list.length; i < len; i++) {
					if (!list[i].isChecked) {
						selectAll = false;
						break;
					}
					selectAll = true;
				}
				return selectAll;
			}

			$scope.selectSingle = function (item, list, type) {
				var selectAll = false;
				if (!item.isChecked) {
					selectAll = false;
				} else {
					if (!checkAll(list)) {
						selectAll = false;
					} else {
						selectAll = true;
					}
				}
				if (angular.isDefined(type)) {
					$scope.markInfo["allChecked" + type] = selectAll;
				} else {
					$scope.markInfo.allChecked = selectAll;
				}
				getBillIds();
			};

			$scope.addPricewarn = function () {
				let selectedDatas = $scope.dataList.filter(df => df.isChecked);
				if ($scope.subTabFlag[1]) {
					selectedDatas = $scope.dataListMateriel.filter(df => df.isChecked);
				}
				if (selectedDatas.length === 0) {
					ybWidget.ShowMessage('请选择需要操作的行');
					return;
				}

				ybWidget.ShowMessage("确定要生成督办单吗？", MESG_TYPE.Ask).then(function () {
					var service = "device";
					var reqUrl = "/priceSchemeWarnCustomer/addDbd";
					if ($scope.subTabFlag[1]) {
						reqUrl = "/priceSchemeWarnMateriel/addDbd";
					}

					ybHttp.Post(service, reqUrl, selectedDatas.map(s => s.id)).then(function (resp) {
						if (resp.code !== 0) {
							ybWidget.ShowMessage(resp, MESG_TYPE.Error);
							return;
						}
						ybWidget.ShowToaster('success', '操作成功');
					});
				})
			};
			/**
			 *
			 * @param {*} ids
			 */
			function handleUpdatePricewarn(ids) {
				ybWidget.ShowConfirm("维护价格方案说明", [{ label: '价格方案变更说明'}]).then(function (data) {
					var service = "device";
					var reqUrl = "/priceSchemeWarnCustomer/update";
					if ($scope.subTabFlag[1]) {
						reqUrl = "/priceSchemeWarnMateriel/update";
					}
					var param = {
						ids: ids,
						priceSchemeRemark: data[0].value,
					};
					ybHttp.Post(service, reqUrl, param).then(function (resp) {
						if (resp.code !== 0) {
							ybWidget.ShowMessage(resp, MESG_TYPE.Error);
							return;
						}
						ybWidget.ShowToaster('success', '操作成功');
						if ($scope.subTabFlag[1]) {
							getDataListMateriel();
						} else {
							getDataList();
						}
					})
				});
			}

			$scope.updatePricewarnBatch = function () {
				let selectedDatas = $scope.dataList.filter(df => df.isChecked);
				if ($scope.subTabFlag[1]) {
					selectedDatas = $scope.dataListMateriel.filter(df => df.isChecked);
				}
				if (selectedDatas.length === 0) {
					ybWidget.ShowMessage('请选择需要操作的行');
					return;
				}
				handleUpdatePricewarn(selectedDatas.map(s => s.id));
			};


			$scope.updatePricewarn = function (item) {
				handleUpdatePricewarn([item.id]);
			};

			$scope.changeSubTab = function (index) {
				$scope.subTabFlag.forEach(x => { x = false });
				$scope.subTabFlag[index] = true;
				if (index == 0) {
					$scope.placeholder = "客户代码/客户名称"
					$scope.exportParam.allUrl = ybHttp.RequestUrl("device", "/priceSchemeWarnCustomer/exportAll");

				}
				if (index == 1) {
					$scope.placeholder = "零件编号/零件中文名"
					$scope.exportParam.allUrl = ybHttp.RequestUrl("device", "/priceSchemeWarnMateriel/exportAll");
				}
			};



			$scope.activeFlag = [true, false, false, false];
			// 切换标签页
			$scope.changeTab = function (index) {
				$scope.activeFlag = [false, false, false, false];
				$scope.activeFlag[index] = true;

				$scope.submitted = false;
			};

			$scope.checkDate = function (obj, startFiled, endFiled) {
				var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
				if (!!timeFormatObj.tStart) {
					obj[startFiled] = timeFormatObj.tStart;
				}
				if (!!timeFormatObj.tEnd) {
					obj[endFiled] = timeFormatObj.tEnd;
				}
			};

			const echartsOption = {
				title: {},
				legend: {
					data: ['常规', '价格折让', '促销活动']
				},
				label: {
					show: true,
					position: 'top',
				},
				xAxis: [
					{
						type: 'category',
						axisLabel: {
							rotate: 45
						},
						data: []
					}
				],
				series: [{ name: '常规', type: 'bar' }, { name: '价格折让', type: 'bar' }, { name: '促销活动', type: 'bar' }],
				dataZoom: [
					{
						type: 'inside',
					}
				]
			};

			$scope.echartsOption1 = angular.copy(echartsOption);
			$scope.echartsOption1.title.text = '价格方案(类型)-时间'

			$scope.echartsOption2 = angular.copy(echartsOption);
			$scope.echartsOption2.title.text = '价格方案(类型)-客户'
			$scope.echartsOption2.xAxis[0].axisLabel.rotate = 90;

			$scope.echartsOption3 = angular.copy(echartsOption);
			$scope.echartsOption3.title.text = '价格方案(零件)-时间'


			$scope.echartsCondition1 = {};
			$scope.echartsCondition2 = {};
			$scope.echartsCondition3 = {};
			$scope.echartsSearch1 = function (valid) {
				if (!valid) {
					$scope.submitted = true;
					return;
				}

				ybHttp.Post('device', '/priceSchemeWarnRpt/queryPriceSchemeWarn1Rpt', $scope.echartsCondition1).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return;
					}
					const resT = resp.value || [];
					$scope.echartsOption1.xAxis[0].data = resT.map(r => r.day);
					$scope.echartsOption1.series[0].data = resT.map(r => r.cg);
					$scope.echartsOption1.series[1].data = resT.map(r => r.tp);
					$scope.echartsOption1.series[2].data = resT.map(r => r.cx);
				});
			};
			$scope.echartsSearch2 = function (valid) {
				if (!valid) {
					$scope.submitted = true;
					return;
				}

				ybHttp.Post('device', '/priceSchemeWarnRpt/queryPriceSchemeWarn2Rpt', $scope.echartsCondition2).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return;
					}
					const resT = resp.value || [];
					$scope.echartsOption2.xAxis[0].data = resT.map(r => r.customerName);
					$scope.echartsOption2.series[0].data = resT.map(r => r.cg);
					$scope.echartsOption2.series[1].data = resT.map(r => r.tp);
					$scope.echartsOption2.series[2].data = resT.map(r => r.cx);
				});
			};
			$scope.echartsSearch3 = function (valid) {
				if (!valid) {
					$scope.submitted = true;
					return;
				}

				ybHttp.Post('device', '/priceSchemeWarnRpt/queryPriceSchemeWarn4Rpt', $scope.echartsCondition3).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return;
					}
					const resT = resp.value || [];
					$scope.echartsOption3.xAxis[0].data = resT.map(r => r.day);
					$scope.echartsOption3.series[0].data = resT.map(r => r.cg);
					$scope.echartsOption3.series[1].data = resT.map(r => r.tp);
					$scope.echartsOption3.series[2].data = resT.map(r => r.cx);
				});
			};
           //价格方案变动预警-编辑
			var btnPermissionCode = {
				priceSchemeWarnEdit: window.ActionCode.priceSchemeWarnEdit,
				priceSchemeWarnToDbd: window.ActionCode.priceSchemeWarnToDbd,
				priceSchemeWarnExport: window.ActionCode.priceSchemeWarnExport,
				priceSchemeWarn: window.ActionCode.priceSchemeWarn,
			};
			$scope.btnPermission = {
				priceSchemeWarnEdit: true,
				priceSchemeWarnToDbd: true,
				priceSchemeWarnExport: true,
				priceSchemeWarn: true,
			};
			function setBtnPermission() {
				for (var key in btnPermissionCode) {
					$scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
				}
			}

			(function () {
				$scope.setFixedWidth();
				//客户
				if ($stateParams.billNo) {
					$scope.condition.id = $stateParams.billNo
					getDataList();
				}else {
					getDataList();
				}
				//零件
				getDataListMateriel();
				UserContextService.SetBtnPermission(setBtnPermission);
			})();
		}])
		;
})(window, window.angular);
